package videostreaminglinux.component;

import java.awt.Graphics;

import java.awt.Image;
import java.awt.Toolkit;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author Jiri Masek, masekj5
 */
public class ImagePanel extends JPanel implements Runnable {

    private Image image;

    public ImagePanel() {
        (new Thread(this)).start();
    }

    public Image getImage() {
        return image;
    }

    public void setImage(byte[] imageArray) {
        System.out.println((new Date()).getTime());
        try {
            image = Toolkit.getDefaultToolkit().createImage(imageArray);
        } catch (Exception ex) {
            System.err.println("Exception: " + ex.getMessage());
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        if (image != null) {
            g.drawImage(image, 0, 0, null);
        }
    }

    public void run() {
        do {
            repaint();
            try {
                Thread.sleep(40);
            } catch (InterruptedException ex) {
                System.err.println("InterruptedException: " + ex.getMessage());
            }
        } while (true);
    }
}
